import { eventCustom } from "./EventCustom";

const { ccclass, property, menu } = cc._decorator;
/**
 * 返回此位置的节点
 */
@ccclass
@menu("自定义组件/ScrollView/选中此位置节点")
export default class ScrollViewSelectItem extends cc.Component {
    @property(cc.ScrollView)
    scrollview: cc.ScrollView = null;

    @property({
        type: cc.Node,
        tooltip: "定位点,返回此位置的子节点"
    })
    pn_TargetPosNode: cc.Node = null;
    content: cc.Node = null;
    _controlPoint
    onLoad() {
        this.content = this.scrollview.content;
        if (!this.pn_TargetPosNode) {
            this._controlPoint = cc.v2()
        } else {
            this._controlPoint = this.pn_TargetPosNode.position
        }
    }
    check() {
        let parentCenter = this.scrollview.node.convertToWorldSpaceAR(this._controlPoint)
        let nodeCenter = this.content.convertToNodeSpaceAR(parentCenter)
        //查找包含吸引点的节点
        let len = this.content.children.length;
        for (let i = 0; i < len; i++) {
            let item = this.content.children[i];
            if (item) {
                if (item.getBoundingBox().contains(nodeCenter)) {
                    // let string = item.getComponent(item.name).lbl_index.string
                    //Mark:发送数据
                    eventCustom.emit("SV_getItem", item)
                    return item
                }
            }
        }
    }
}
